package mymac.com.test_3_1;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;

/**
 * Created by mymac on 2017/2/24.
 */

public class LoginActivity extends Activity {
    private EditText et_name,et_pwd;
    private Button btn_login;
    private SharedPreferences preferences;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_name = (EditText) findViewById(R.id.et_name);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        btn_login = (Button) findViewById(R.id.btn_login);
        preferences = getSharedPreferences("token",Activity.MODE_PRIVATE);
        et_name.setText(preferences.getString("userName",""));
        et_pwd.setText(preferences.getString("password",""));
    }

    public void click(View view){
        //进度框
        progressDialog = ProgressDialog.show(LoginActivity.this,"温馨提示","正在登录...");
        //编辑器
        SharedPreferences.Editor editor = preferences.edit();
        final String name = et_name.getText().toString().trim();
        final String pwd = et_pwd.getText().toString().trim();
        final String address = "http://a.xl18z.cn/api/login?name=" + name + "&password=" + pwd;
        HttpActivity.getInstance().getJson(address, new DataCallBack() {
            @Override
            public void onErr(Object object) {
                progressDialog();
                JSONObject json = (JSONObject) object;
                String errcode = json.optString("errcode");
                if ("0".equals(errcode)) {
                    showToast(json);
                    //获取token
                    JSONObject record = json.optJSONObject("record");
                    String token = record.optString("token");
                    savePreferences(token, name, pwd);
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    progressDialog();
                    showToast(json);
                }
            }
        });
    }
    //关闭提示框
    public void progressDialog(){
        progressDialog.dismiss();
    }
    //显示吐司
    public void showToast(JSONObject jsonObject){
        Toast.makeText(LoginActivity.this, ""+jsonObject.optString("errmsg"), Toast.LENGTH_SHORT).show();
    }
    //保存token、name、pass
    public void savePreferences(String token,String name,String psw){
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("token",token);
        editor.putString("userName",name);
        editor.putString("password",psw);
        editor.commit();
    }
}
